package com.hmall.order.web;

import com.hmall.common.myClient.ItemClient;
import com.hmall.common.myClient.UserClient;
import com.hmall.common.pojo.Address;
import com.hmall.common.pojo.Item;
import com.hmall.order.amqp.MqConstants;
import com.hmall.order.config.ThreadLocalUtil;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.Parpm;
import com.hmall.order.service.IOrderService;
import com.hmall.order.service.OrderDetailService;
import com.hmall.order.service.OrderLogisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

   @Autowired
   private IOrderService orderService;
   @Autowired
   private OrderDetailService orderDetailService;
   @Autowired
   private OrderLogisticsService orderLogisticsService;

   @Autowired
   private RabbitTemplate rabbitTemplate;

   @Autowired
   private UserClient userClient;

   @Autowired
   private ItemClient itemClient;


   @GetMapping("/{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId, HttpServletRequest request) {
      log.info("authorization-----orderService中的------------->{}",request.getHeader("authorization"));
      Order order = orderService.getById(orderId);
      log.info("test-----orderService中的------------->{}",request.getHeader("test"));

      return order;
   }



   @PostMapping
   @Transactional(rollbackFor = Exception.class)
   public Long findorderId(@RequestBody Parpm parpm){
      Integer num = parpm.getNum();
      Long itemId = parpm.getItemId();
      Long addressId = parpm.getAddressId();
      Integer type = parpm.getPaymentType();

      Item item = itemClient.getById(itemId);
      //获取请求头里的用户id(就是2)
      Long userId = ThreadLocalUtil.getCurrentId();
      Long price = item.getPrice();
      Long totalFee = price * num;
      Order order = new Order();
      order.setTotalFee(totalFee);
      order.setStatus(1);
      order.setPaymentType(type);
      order.setUserId(userId);

      Address address = userClient.findAddressById(addressId);




      //添加订单
      boolean saveOrder = orderService.save(order);
      log.info("order订单创建成功:{}", order+"------"+saveOrder);

      OrderDetail orderDetail = new OrderDetail();
      Long orderId = order.getId();
      String spec = item.getSpec();
      String image = item.getImage();
      orderDetail.setOrderId(orderId);
      orderDetail.setImage(image);
      orderDetail.setNum(num);
      orderDetail.setItemId(itemId);
      orderDetail.setPrice(price);
      orderDetail.setSpec(spec);
      orderDetail.setName(item.getName());

      //添加订单详情
      boolean orderDetailServiceSave = orderDetailService.save(orderDetail);
      log.info("orderDetail订单详情创建成功:{}", orderDetail+"------"+orderDetailServiceSave);

      OrderLogistics orderLogistics = new OrderLogistics();
      orderLogistics.setOrderId(orderId);
      orderLogistics.setContact(address.getContact());
      orderLogistics.setProvince(address.getProvince());
      orderLogistics.setCity(address.getCity());
      orderLogistics.setTown(address.getTown());
      orderLogistics.setPhone(address.getMobile());
      orderLogistics.setStreet(address.getStreet());

      //订单派送地址表
      boolean saveOrderLogistics = orderLogisticsService.save(orderLogistics);
      log.info("orderLogistics----->创建成功:{}", orderLogistics+"------"+saveOrderLogistics);
      itemClient.reduceInventory(itemId, num);

      if (saveOrder &&orderDetailServiceSave && saveOrderLogistics){
         System.out.println("order的雪花算法生成的id是:"+order.getId());
         rabbitTemplate.convertAndSend(MqConstants.ORDER_EXCHANGE, MqConstants.ORDER_ROUTINGKEY, orderId);
         log.info("order发送消息的时间是：{}",  new Date()+"----------");
      }

      return orderId;
   }


}
